from datetime import datetime
from Common import sql_content

class Job_map(object):
    def __init__(self):
        self.count = 0
        self.sql_content = sql_content.sql_content()
        self.area_all = []
        # self.country = {'北京':'beijing','上海':'sahnghai' ,'天津':'tianjin','重庆':'chongqing','黑龙江':'heilongjiang', '吉林':'jilin',
        #                   '辽宁':'liaoning', '内蒙古':'neimenggu','河北':'hebei' ,'新疆':'xinjiang', '甘肃':'gansu',
        #                   '青海':'qinghai','陕西': 'shanxi3' ,'宁夏':'ningxia', '河南':'henan' ,'山东':'shandong','山西':'shanxi1',
        #                   '安徽':'anhui', '湖南':'hunan','湖北':'hubei' ,'江苏':'jiangsu','四川':'sichuan' ,'贵州':'guizhou',
        #                   '云南':'yunnan', '广西':'guangxi','西藏' :'xizang' ,'浙江':'zhejiang', '江西':'jiangxi' ,'广东':'guangdong' ,
        #                 '哈尔滨':'haerbin','长春':'changchun','沈阳':'shenyang','呼和浩特':'huhehaote','石家庄':'shijiazhuang',
        #                 '乌鲁木齐':'wulumuqi','兰州':'lanzhou','西宁':'xining','西安':'xian','银川':'yinchuan','郑州':'zhengzhou',
        #                 '济南':'jinan','太原':'taiyuan','合肥':'hefei','长沙':'changsha','武汉':'wuhan','南京':'nanjing','成都':'chengdu',
        #                 '贵阳':'guizhou','昆明':'kunming','南宁':'nanning','拉萨':'lasa','杭州':'hangzhou','南昌':'nanchang',
        #                 '广州':'guangzhou'
        # }
        self.country = {'北京':'北京','上海':'上海' ,'天津':'天津','重庆':'重庆','黑龙江':'哈尔滨', '吉林':'长春',
                          '辽宁':'沈阳', '内蒙古':'呼和浩特','河北':'石家庄' ,'新疆':'乌鲁木齐', '甘肃':'兰州',
                          '青海':'西宁','陕西': '西安' ,'宁夏':'银川', '河南':'郑州' ,'山东':'济南','山西':'太原',
                          '安徽':'合肥', '湖南':'长沙','湖北':'武汉' ,'江苏':'南京','四川':'成都' ,'贵州':'贵阳',
                          '云南':'昆明', '广西':'南宁','西藏' :'拉萨' ,'浙江':'杭州', '江西':'南昌' ,'广东':'广州' ,
        }
        self.country_count = {
            'beijing', 'shanghai','tianjin','chongqing', 'heilongjiang', 'haerbin', 'jilin' ,'changchun',
            'liaoning','shenyang', 'neimenggu','huhehaote', '河北', '石家庄', '新疆', '乌鲁木齐', '甘肃','兰州',
            '青海', '西宁', '陕西', '西安', '宁夏', '银川', '河南', '郑州', '山东', '济南', '山西', '太原',
            '安徽', '合肥', '湖南', '长沙', '湖北', '武汉', '江苏', '南京', '四川', '成都', '贵州', '贵阳',
            '云南', '昆明', '广西', '南宁', '西藏', '拉萨', '浙江', '杭州', '江西', '南昌', '广东', '广州',
            '福建', '福州', '台湾', '台北', '海南', '海口', '香港', '香港', '澳门', '澳门'
        }
        self.area_count_dict = {}
        self.atea_count_dict_new = {'other':0}
        self.this_dict = {}
# 小米社招Job_place
    def select_area_she(self):
        con = self.sql_content.conten()
        cursor = con.cursor()
        sql_xiaomi_she = "select Job_Place from data_xiaomi_she"

        cursor.execute(sql_xiaomi_she)
        rs = cursor.fetchall()
        for row in rs:
            #print("Job_Place=%s" % row)#%---ceshi---%
            self.area_all.append(row[0])
        #print(cursor.rowcount)

        cursor.close()
        con.close()

# 小米校招Job_place
    def select_area(self):
        con = self.sql_content.conten()
        sql_xiaomi = "select Job_Place from data_xiaomi"
        cursor = con.cursor()
        cursor.execute(sql_xiaomi)
        rs_1 = cursor.fetchall()
        for row_1 in rs_1:
            #print("hhhhh=%s" % row_1)  # %---ceshi---%
            self.area_all.append(row_1[0])
        cursor.close()
        con.close()
#51社招Job_Place
    def select_area_51job(self):
        con = self.sql_content.conten()
        sql_51job = "select Job_Place from data_51job_she"
        cursor = con.cursor()
        cursor.execute(sql_51job)
        rs = cursor.fetchall()
        for row in rs:
            #print("hhhhh=%s" % row)  # %---ceshi---%
            self.area_all.append(row[0])
        cursor.close()
        con.close()


    def select_main(self):
        self.select_area_she()
        self.select_area()
        self.select_area_51job()
        #(self.area_all)# %---ceshi---%
        #print(len(self.area_all))# %---ceshi---%
        #print('这是mantoo啊' ,set(self.area_all) , len(set(self.area_all)))
        area_set = (set(self.area_all))
        for area in area_set:
            area_count = self.area_all.count(area)
            self.area_count_dict[area] = area_count
        #print(self.area_count_dict)
        # hhh = 0
        # for v in self.area_count_dict:
        #     hhh += self.area_count_dict[v]
        # print(hhh)

        for one in self.country:
            count_country = 0
            for two in self.area_count_dict:
                if(two.count(self.country[one]) > 0):
                    count_country += self.area_count_dict[two]

            if(count_country != 0):
                self.atea_count_dict_new[self.country[one]] = count_country
            if (count_country == 0):
                #self.atea_count_dict_new['other'] += 1000
                pass
        # print('看可以' ,self.atea_count_dict_new)
        # hhh = 0
        # for v in self.atea_count_dict_new:
        #     hhh += self.atea_count_dict_new[v]
        # print(hhh)

        num_dict = []
        for k in self.atea_count_dict_new:
            num_dict.append(self.atea_count_dict_new[k])
        #print(num_dict)
        num_dict.sort(reverse=True)
        fill_count = 0
        for s in range(10,len(self.atea_count_dict_new)):
            fill_count += num_dict[s]
        print(num_dict)
        num_dict.append(fill_count)
        for x in self.atea_count_dict_new:
            for l in num_dict:
                if(self.atea_count_dict_new[x] == l):
                    self.this_dict[x] =l
        #self.this_dict['other'] = num_dict[11]
        #print(self.atea_count_dict_new)
        print(self.this_dict)
        #print(num_dict)

        return self.this_dict

    def name_main(self):
        page = {}
        page['title'] = '就业岗位最多'
        page['int'] = """
                    大多数的人一开始选择的城市就是自己奋斗一生的城市，据了解，七成的职场白领轻易不会放弃自己已经积累的事业基础和人脉关系，轻易不会更换城市，除非另外一个城市会有更好的发展或者有爱情的吸引。因此，每个人在选择一个城市的时候无不经过深思熟虑，那么，参与调查的白领最心仪的就业城市是哪几个呢？
　　“大学生就业向往城市排名”前十名分别为：上海（15.12％）、北京（14.53％）、杭州（8.72％）、广州（7.56％）、深圳（6.40％）、成都（6.40％）、大连（4.65％）、苏州（4.65％）、青岛（4.07％）、武汉（3.49％）、西安（3.49％）、厦门（3.49％）。
　　随着近几年的迅速发展和人们就业观念的转变，杭州成为继上海、北京之后深受白领欢迎的就业城市，广州、深圳不复当年风光，跌出前三甲。被某周刊评为“中国第五城”的成都，因其人杰地灵、美食＋美女，吸引了众多白领，和深圳并列第五名。"""
        page['type'] = '毕业生,就业岗位, 10大城市, 图标, 饼状图'
        page['unknow'] = 'view'
        page['date'] = '2017/8/27'
        page['list'] = {'小米招聘': 'www', '华为招聘': 'www', '阿里招聘': 'www', '百度招聘': 'www', '51招聘': 'www'}

        if(self.count != 0):
            pass
        elif(self.count == 0 or datetime.now().strftime('%H:%M') == '00:00'):
            area_main = Job_map()
            all_area_count = area_main.select_main()
            self.count += 1

        return all_area_count,page

if __name__ == "__main__":
    area_main = Job_map()
    area_main.select_main()

    #print(time.strftime('%Y-%m-%d', time.loadtime(time.time())))